package com.machinsight.library.action;

import com.machinsight.library.bean.LoginUser;
import com.machinsight.library.content.ContentValue;
import com.machinsight.library.controller.LoginBusinessController;
import com.machinsight.library.dao.bean.UserInfoEntity;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

/**
 * Copyright ©2017-2019 Machinsight Technology Co., Ltd.All rights reserved.
 * Author: Zhoushaohua
 * Date: 2019/12/27
 * Description:登录action
 */
public class LoginAction extends ActionSupport {
    private UserInfoEntity userInfoEntity;

    public String doLogin() {
        //获取HttpServletRequest对象
        HttpServletRequest request = ServletActionContext.getRequest();
        //这里的key值"username"、"password"与表单中的一致
        String username = request.getParameter("name");
        String pwd = request.getParameter("password");
        if (username == null || username.isEmpty()
                || pwd == null || pwd.isEmpty()) {
            throw new RuntimeException("账户或密码不能为空！");
        }
        //创建出唯一的登录业务相关controller
        LoginBusinessController loginController = new LoginBusinessController();
        LoginUser user = new LoginUser();
        user.setName(username);
        user.setPassword(pwd);
        userInfoEntity = loginController.queryUserByNameAndPwd(user);

        if (userInfoEntity == null) {
            throw new RuntimeException("账户或密码错误！");
        }
        System.out.println(userInfoEntity.toString());
        return ContentValue.SUCCESS;
    }

    public UserInfoEntity getUserInfoEntity() {
        return userInfoEntity;
    }

    public void setUserInfoEntity(UserInfoEntity userInfoEntity) {
        this.userInfoEntity = userInfoEntity;
    }
}
